;(function(document, win, undefined) {
    function carousel(config) {
        this.config = config;
        this.init();

    }
    carousel.prototype =  {
        init: function() {
            this.i = 0;
            this.timer = null;
            this.sliderContaienr = document.createElement("div");
            this.sliderPrev = document.createElement("div");
            this.sliderNext = document.createElement("div");
            this.sliderImages = document.createElement("div");
            this.sliderImages.style.width = this.config.imgArr.length * 800 + "px";
            for(var i = 0; i < this.config.imgArr.length; i++){
                 var imgEle = document.createElement("img");
                 imgEle.src = './img/'+ this.config.imgArr[i];
                 this.sliderImages.appendChild(imgEle);
            }
            this.sliderPointerItem = document.createElement("div");
            for(var j = 0; j < this.config.imgArr.length; j++){
                var iEle = document.createElement("i");
                iEle.className = "slider--pointer__item";
                this.sliderPointerItem.appendChild(iEle);
                if(j === 0){
                    iEle.className = "slider--pointer__item hightLight";
                }
            }
            this.setClass();
        },
        setClass: function() {
            this.sliderContaienr.className = "slider--container";
            this.sliderPrev.className = "slider--prev";
            this.sliderNext.className = "slider--next";
            this.sliderImages.className = "slider--images";
            this.sliderPointerItem.className = "slider--pointer";
            this.render();
        },
        render: function() {
            this.sliderContaienr.appendChild(this.sliderPrev);
            this.sliderContaienr.appendChild(this.sliderNext);
            this.sliderContaienr.appendChild(this.sliderImages);
            this.sliderContaienr.appendChild(this.sliderPointerItem);
            document.body.appendChild(this.sliderContaienr);
            this.autoPlay();
            this.binEv();
        },
        autoPlay:function(){
            var self = this;
            self.timer = setInterval(function () {
                self.i++;
                self.moveToIndex(self.sliderImages,self.i);
            },2000);
        },
        moveToIndex:function(dom, index){
            dom.style.left = -800 * (index % this.config.imgArr.length) + "px";
            this.highLightPointer();
        },
        binEv:function(){
            var self = this;
            self.sliderContaienr.onmouseover=function(){ self.stopAutoPlay()};
            self.sliderContaienr.onmouseout=function(){ self.autoPlay()};
            self.sliderPrev.onclick=function(){self.prev()};
            self.sliderNext.onclick=function(){self.next()};
        },
        prev:function(){
            this.i--;
            this.detect();
            this.moveToIndex(this.sliderImages,this.i)
        },
        next:function(){
            this.i++;
            this.moveToIndex(this.sliderImages,this.i)
        },
        stopAutoPlay:function(){
            clearInterval(this.timer);
        },
        /*左箭头删除*/
        detect:function(){
            if(this.i < 0){
                this.i = this.config.imgArr.length - 1;
            }
        },
        highLightPointer:function(){
            for (var k = 0; k < this.sliderPointerItem.children.length; k ++) {
                this.sliderPointerItem.children[k].className = 'slider--pointer__item' + (k === (this.i % this.config.imgArr.length) ? ' hightLight' : '');
            }
        }

    }
    win['carousel'] = carousel;
})(document, window);